<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格展示 用到第三方组件 vue-table-with-tree-grid -->
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <!-- 是否有效模板 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" style="color:lightgreen;" v-if="scope.row.cat_deleted===false"></i>
          <i class="el-icon-error" v-else style="color:red;"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag type="success" size="mini" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag type="danger" size="mini" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <!-- 添加分类弹出框 -->
      <el-dialog @close="addCateDialogClose" title="添加分类" :visible.sync="addDialogVisible" width="50%">
        <!-- 添加分类表单 -->
        <el-form
          :model="addCateForm"
          :rules="addCateFormRules"
          ref="addCateFormRef"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>
          <!-- 级联选择框 -->
          <el-form-item label="选择分类">
            <el-cascader
              v-model="selectKeys"
              :options="options"
              :props="cascaderProps"
              @change="handleChange"
              clearable
            ></el-cascader>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //查询参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      //查询列表
      cateList: [],
      //总条数
      total: 0,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok"
        },
        {
          label: "排序",
          type: "template",
          template: "order"
        },
        {
          label: "操作",
          type: "template",
          template: "opt"
        }
      ],
      //添加分类弹出框展示
      addDialogVisible: false,
      //添加分类表单
      addCateForm: {
        cat_pid: 0, //分类父 ID不能为空，如果要添加1级分类，则父分类Id应该设置为  `0`
        cat_name: '', //分类名称不能为空
        //由于element ui版本高 无法选中二级分类
        cat_level: 0 //分类层级不能为空，`0`表示一级分类；`1`表示二级分类；`2`表示三级分类
      },
      //校验规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: "请选择分类名称", trigger: "blur" }
        ]
      },
      //级联数据绑定
      options: [],
      selectKeys: [],
      //级联展示规则
      cascaderProps: {
        expandTrigger: "hover",
        label:'cat_name',
        value:'cat_id',
        children:'children'
      }
    };
  },
  created() {
    //获取商品列表
    this.getCateList();
  },
  methods: {
    //条数发生变化
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize;
      this.getCateList();
    },
    //页码发生变化
    handleCurrentChange(newpage) {
      this.queryInfo.pagenum = newpage;
      this.getCateList();
    },
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取商品列表失败");
      this.cateList = res.data.result;
      this.total = res.data.total;
    },
    //添加分类
    showAddCate() {
      //加载级联数据
      this.getCascaderList();
      this.addDialogVisible = true;
    },
    //获取级联数据
    async getCascaderList() {
      const { data: res } = await this.$http.get("categories", {
        params: {
          type: 2
        }
      });
      if(res.meta.status!==200)return this.$message.error('获取商品分类失败')
      this.options = res.data;
      
    },
    //级联数据发生变化
    handleChange() {
      if(this.selectKeys.length>0){
        //如果selectKeys数组不为空 则是选择了分类
        this.addCateForm.cat_pid=this.selectKeys[this.selectKeys.length-1];
        this.addCateForm.cat_level=this.selectKeys.length;
      }else{
        //否则是没有选择分类
        this.addCateForm.cat_pid=0;
        this.addCateForm.cat_level=0;
      }
      
    },
    //关闭弹出框
    addCateDialogClose(){
      this.$refs.addCateFormRef.resetFields();
      this.selectKeys=[]
    },
    //添加分类
    addCate(){
        this.$refs.addCateFormRef.validate(async valid=>{
          if(!valid)return
          const {data:res} = await this.$http.post('categories',this.addCateForm);
          if(res.meta.status!==201)return this.$message.error('创建失败');
          this.$message.success('创建成功')
          this.getCateList();
          this.addDialogVisible=false;
        })
    }
  }
};
</script>
<style scope>
</style>